package cn.marinda.spritefragment.commands;

import cn.marinda.spritefragment.data.ConfigManager;
import cn.marinda.spritefragment.data.DataInterface;
import cn.marinda.spritefragment.util.PlayerUtil;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class ResultCommand implements CommandsObject{
    private ConfigManager gm = null;
    @Override
    public boolean registerCommands(String cmd, String[] args) {
        if (args[0].equalsIgnoreCase("result")) {
            for (Player player : PlayerUtil.playerOnlineList) {
                if (args[1].equalsIgnoreCase(player.getName())) {
                    try {
                        gm = new ConfigManager();
                        DataInterface di = gm.getItemData();
                        ItemStack item = di.getResultItems(args[2]);
                        int number = Integer.parseInt(args[3]);
                        item.setAmount(number);
                        if (player.getInventory().firstEmpty() == -1) {
                            player.sendMessage("§6" +gm.getConfigData().getPrefix() +"背包满了");
                            return true;
                        }
                        player.getInventory().addItem(item);
                        player.sendMessage("§6" +gm.getConfigData().getPrefix() +"§a你获得了§b" + args[2] + "物品");
                        return true;
                    }catch(NullPointerException e){
                        player.sendMessage("§6" +gm.getConfigData().getPrefix() +"§c没有这个代号存在哦");
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
